Elements of a given array A can be altered using the assignment expression A(indexArray) = b. Since there is just one index array in this expression, elements are indexed linearly. Elements to be changed are specified by indexes contained in indexArray. The new values are specified in the array b. Both A and b should have the same data type.
As we will see below, the behaviour of the assignment varies, depending on the sizes of A, indexArray and b. Our objective here is to exhaust all possible scenarios for the sake of completeness.
Let's start with a very simple example below, in which just one element is changed. The 2nd element in the vector A is changed to -1:
A = [1, 2, 3, 4];
% 2nd element of A.
A(2) = -1
A =
1.0000 -1.0000 3.0000 4.0000
To change multiple elements in one assignment operation, use an index array that contains multiple indexes.
A = [1, 2, 3, 4];
% Multiple indexes.
A([1 2]) = [0 -1]
A =
0.0000 -1.0000 3.0000 4.0000
If an index exceeds the length of A, trailing zeros will be appended to A before the assignment.
⚠️ This only works when
Ais a vector, since linear indexes do not convey size information of matrices or multi-dimensional arrays.
% A has length 4
A = [1 2 3 4];
% The indexes 5 and 7 below exceed the length of A
% Trailing zeros will be appended before the assignment
% to create a vector of length 7.
A([4 5 7]) = rand(1,3)
A =
Columns 1 through 3:
1.0000 2.0000 3.0000
Columns 4 through 6:
0.2604 0.6395 0.0000
Column 7:
0.2404
indexArraay and bIn the assignment A(indexArray) = b, the arrays indexArray and b are first reshaped to two vectors before the assignment. The sizes of indexArray and b can be different, but they must have same number of elements.
However, this requirement can be relaxed when b is a scalar or empty. If b is a scalar, it is expanded to have the same number of elements as indexArray. If b is empty, it is called null assignment, which will be discussed in the section ”Empty b” below.
In the example below, indexArray and b are matrices of 4 numbers. Their sizes are not important since they will be reshaped to vectors anyway before the assignment.
R = rand(2, 2, 3);
% Before assignment,
% The index array [1 2; 11 12]
% and the right-hand side [1 2; 3 4] are first reshaped
% to [1 11 2 12] and [1 3 2 4], respectively.
% After the assignment below,
% A(1) == 1, A(11) == 3, A(2) == 2, A(12) == 4
A = R
disp('After assignment:')
A([1 2; 11 12]) = [1 2; 3 4]
% Since the size of index array is not important,
% A([1 11 2 12]) = [1 2; 3 4] below gives the same result.
A = R;
disp('After assignment:')
A([1 11 2 12]) = [1 2; 3 4]
A(:, :, 1) = 1e-1 ×
3.7755 1.0963
0.0876 9.7137
A(:, :, 2) = 1e-1 ×
8.2139 0.6574
0.2630 9.3532
A(:, :, 3) = 1e-1 ×
3.9112 7.4746
2.4390 8.3984
After assignment:
A(:, :, 1) =
1.0000 0.1096
2.0000 0.9714
A(:, :, 2) =
0.8214 0.0657
0.0263 0.9353
A(:, :, 3) =
0.3911 3.0000
0.2439 4.0000
After assignment:
A(:, :, 1) =
1.0000 0.1096
2.0000 0.9714
A(:, :, 2) =
0.8214 0.0657
0.0263 0.9353
A(:, :, 3) =
0.3911 3.0000
0.2439 4.0000
AIn the assignment A(indexArray) = b, if A does not exist yet, it will be created as a vector with the shortest length that fits the given index array and the right-hand side b.
⚠️ It is impossible to create non-vectors using linear indexing, since linear indexes convey no information about the dimension lengths. For instance,
A(16) = 3could meanA(4, 4) = 3orA(1,16) = 3. For the former,Ashould be expanded to the size[4, 4]. For the latter, it should be[1, 6]. Such an assignment is ambiguous and is therefore prohibited.
In the example below, all variables are first cleared in order to make sure that A does not exist yet. Then, A is created by an assignment operation. The length is determined by the maximum value in the index array, which is 8. Un-indexed elements are filled with zeros.
clear
A([4 6 8]) = [-1 -2 -3]
All variables cleared
A =
Columns 1 through 5:
0.0000 0.0000 0.0000 -1.0000 0.0000
Columns 6 through 8:
-2.0000 0.0000 -3.0000
In the following example, the assignment has the form A(:) = b, in which colon : is used instead of an index array. The array A is created to have the same length as b. We can also use A(1:end) = b or A = b, which will give identical results.
clear
A(:) = [-1 -2 -3]
B = [-1 -2 -3]
C(1:end) = [-1 -2 -3]
All variables cleared.
A =
-1.0000 -2.0000 -3.0000
B =
-1.0000 -2.0000 -3.0000
C =
-1.0000 -2.0000 -3.0000
A Too ShortWhen A is a vector, it can be expanded to contain more elements. In the code below, A has length 5 before the assignment. The indexes 7 and 8 exceed the length of A. Hence, A is expanded to accommodate the extra elements. However, when A is a matrix or ndims(A) >= 3, the size after expansion is ambiguous. In this case, the expansion of A is not allowed.
% Expansion is allowed
A = 1:5;
A([3 7 8]) = -1
% Expansion is not allowed
A = [1 2 3; 4 5 6];
A([7, 8]) = -1
A =
1.0000 2.0000 -1.0000 4.0000 5.0000 0.0000 -1.0000 -1.0000
Error at Line 6(1) of the Console. Sizes of matrices and multi-dimensional arrays cannot be expanded in an array assignment using linear indexing.
indexArrayAn empty index array simply means that none of the elements will be indexed. Therefore, if an empty index array is given and the right-hand side is not empty, the array remains unchanged. This is demonstrated below.
A = [1 2 3; 4 5 6];
% Not assigning any value since the index array is empty.
A([]) = 1
% Not assigning any value since the index array is empty.
A([]) = [6 7; 8 9]
A =
1.0000 2.0000 3.0000
4.0000 5.0000 6.0000
A =
1.0000 2.0000 3.0000
4.0000 5.0000 6.0000
When the right-hand side b is empty, it means null assignment, i.e., deleting elements from A. As we shall see in the section "Empty b" below, A is reshaped to a vector after null assignment. Therefore, A([]) = [] simply reshapes A to a vector without deleting any element from it.
A = rand(2,3,2)
A([]) = []
A(:, :, 1) = 1e-1 ×
1.6863 1.7299 2.2695
2.8992 9.9330 9.7120
A(:, :, 2) = 1e-1 ×
7.0733 0.9167 7.5506
0.7294 8.6381 9.3932
A = 1e-1 ×
Columns 1 through 5:
1.6863 2.8992 1.7299 9.9330 2.2695
Columns 6 through 10:
9.7120 7.0733 0.7294 0.9167 8.6381
Columns 11 through 12:
7.5506 9.3932
bWe can delete elements from the array A by using an empty right-hand side b. This is called null assignment. After deleting elements from A, it becomes a vector to avoid situations where A may have invalid size after deletion.
A = rand(1, 3, 2)
disp('A becomes a vector after deletion')
A([1 5]) = []
A(:, :, 1) = 1e-1 ×
8.7805 6.3407 5.8518
A(:, :, 2) = 1e-1 ×
5.7757 2.4256 1.2096
A becomes a vector after deleting
A = 1e-1 ×
6.3407 5.8518 5.7757 1.2096
bSometimes, it is desirable to change multiple elements to the same value. In this case, we use a scalar b on the right-hand side of the assignment. In the code below, we change the values to 6 at both indexes 1 and 2.
v = [1, 2, 3, 4];
v([1 2]) = 6
v =
6.0000 6.0000 3.0000 4.0000